package com.zdkj.edge.door.demo.model.api;

import com.alibaba.fastjson.JSON;
import com.spicdt.iedge.psdk.data.DeviceAlarm;
import com.spicdt.iedge.psdk.meta.AlarmDetail;
import com.spicdt.iedge.psdk.meta.AlarmLevel;
import com.spicdt.iedge.psdk.meta.AlarmStatus;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

/**
 * 响应数据：门禁通行记录响应对象
 */
@Data
@NoArgsConstructor
public class ApiAccessRecordResponse {
  /** 通行记录唯一标识 */
  private int id;

  /** 访客姓名 */
  private String visitorName;

  /** 访客ID */
  private int visitorId;

  /** 通行设备ID */
  private int tdDeviceId;

  /** 通行时间（格式：yyyy-MM-dd HH:mm:ss） */
  private String brushTime;

  /** 通行类型（如刷卡、刷脸等） */
  private String brushType;

  /** 抓拍图片URL */
  private String imagePic;

  /** 进出类型（0-出，1-进） */
  private int devInOut;

  /** 地点名称 */
  private String locationName;


  /**
   * 将通行记录转换为设备告警对象（如用于特殊告警逻辑）
   * @param deviceKey 设备唯一标识
   * @return DeviceAlarm 告警对象
   */
  public DeviceAlarm toAlarm(String deviceKey) {
    DeviceAlarm alarm = new DeviceAlarm();
    alarm.setAlarmDeviceCode(deviceKey);
    alarm.setAlarmCode("access_alarm_" + id);
    alarm.setType("AccessControlAlert");
    alarm.setLevel(AlarmLevel.MINOR);
    alarm.setStatus(AlarmStatus.ACTIVE_UNACK);
    alarm.setStartTs(System.currentTimeMillis());
    Map<String, Object> detailMap = new HashMap<>();
    detailMap.put("visitorName", visitorName);
    detailMap.put("brushTime", brushTime);
    detailMap.put("brushType", brushType);
    detailMap.put("devInOut", devInOut == 1 ? "进入" : "离开");
    detailMap.put("locationName", locationName);
    detailMap.put("imagePic", imagePic);
    detailMap.put("tdDeviceId", tdDeviceId);
    alarm.setDetails(JSON.toJSONString(detailMap));

    alarm.setCreatedTime(System.currentTimeMillis());
    alarm.setPropagate(false);
    return alarm;
  }
}